<template>
	<uni-popup ref="popup" type='bottom' :mask-click="false">
		<view class="pop-contain">
			<view class="pop-top">
				<view style="width: 60px;">
					<uni-icons style="font-size: 22px;" @click="closePopup" type="closeempty"></uni-icons>
				</view>
				<text>请选择类型</text>
				<view style="width: 60px;"
					:style="{'font-size': '15px','color':value === null ? themeColor:'black','font-weight':'bold'}"
					@click="clear">全部类型</view>
			</view>

			<view class="pop-label" v-if="categoryList != null && categoryList.length>=0">
				<view :style="{backgroundColor: themeColor,opacity: typeKey === item.key ? 1:0.3}"
					class="pop-label-item" v-for="(item,index) in categoryList" @click="selectLabel(item)">
					{{item.key}}
				</view>
			</view>

			<scroll-view>
				<view style="height: 350px;overflow: scroll;">
					<view @touchmove.stop @touch.stop class="pop-category">
						<view class="pop-category-item"
							:style="{backgroundColor:value === item.value ? themeColor :'white',color:value === item.value ? 'white' :'black'}"
							v-for="(item,index) in dataList" @click="selectCategory(item)">
							<text class="pop-category-item-text">{{item.childKey}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</uni-popup>
</template>

<script>
	export default {
		name: 'categorySelect',
		props: {
			value: {
				type: Number,
				default: null
			},
			themeColor: {
				type: String,
				default: "#42b479"
			},
			categoryList: {
				default: [],
				type: Object //[{key:"一级标签",values:[{childKey:"二级标签",value:"值"}]}]
			}
		},
		data() {
			return {
				typeKey: "",
				dataList: []
			}
		},
		methods: {
			selectCategory(item) {
				this.$emit('getValue', item)
			},
			selectLabel(item) {
				this.typeKey = item.key
				this.dataList = item.values
			},
			clear() {
				this.$emit('getValue', null)
			},
			openPopup() {
				if (this.categoryList != null && this.categoryList.length >= 0) {
					this.selectLabel(this.categoryList[0])

				}
				this.$refs.popup.open()
			},
			closePopup() {
				this.$refs.popup.close()
			}
		}
	}
</script>

<style scoped lang="scss">
	.pop-contain {
		height: 450px;
		padding: 5px;
		border-radius: 6px 6px 0 0;
		background-color: rgba(250, 250, 250, 1);
	}

	.pop-top {
		font-size: 18px;
		padding: 15px 20px 20px 15px;
		display: flex;
		justify-content: space-between;
		border-bottom: 2px dashed #eee;
	}

	.pop-label {
		display: flex;
		padding-top: 8px;
		padding-bottom: 8px;
		flex-wrap: nowrap;
		overflow: auto;
		text-align: center;
		align-items: center;

		.pop-label-item {
			flex-shrink: 0;
			margin-right: 8px;
			color: white;
			border-radius: 3px;
			padding: 7px 15px 7px 15px;
			font-size: 15px;

			display: flex;
			text-align: center;
			justify-content: center;
			align-items: center;
		}
	}

	.pop-label::-webkit-scrollbar {
		display: none;
	}

	.pop-category::-webkit-scrollbar {
		display: none;
	}

	.pop-category {
		border-radius: 2px;
		display: flex;
		flex-wrap: wrap;

		.pop-category-item {
			margin: 5px;
			flex-basis: 30%;
			height: 30px;
			padding-top: 10px;
			padding-bottom: 10px;
			border-radius: 5px;
			flex-shrink: 0;
			/* 不允许缩小 */

			display: flex;
			justify-content: center;
			align-items: center;


			.pop-category-item-text {
				font-size: 17px;
				text-align: center;
				overflow: hidden;
				flex-wrap: nowrap;
				text-overflow: ellipsis;
			}
		}
	}

	.pop-category-item:active {
		font-weight: bold;
	}
</style>